package com.learning.platform.repository;

import com.learning.platform.entity.Course;
import com.learning.platform.enums.CourseStatus;
import com.learning.platform.enums.Difficulty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Repository
public interface CourseRepository extends JpaRepository<Course, Long> {
    
    /**
     * 分页查询已发布课程（支持关键词搜索）
     */
    @Query("SELECT c FROM Course c WHERE c.status = :status " +
           "AND (:keyword IS NULL OR c.title LIKE %:keyword% OR c.description LIKE %:keyword%) " +
           "ORDER BY c.createdAt DESC")
    Page<Course> findPublishedCourses(@Param("status") CourseStatus status, 
                                    @Param("keyword") String keyword, 
                                    Pageable pageable);
    
    /**
     * 查找教师创建的所有课程
     */
    List<Course> findByTeacherIdOrderByCreatedAtDesc(Long teacherId);
    
    /**
     * 分页查询教师课程
     */
    Page<Course> findByTeacherIdOrderByCreatedAtDesc(Long teacherId, Pageable pageable);
    
    /**
     * 根据分类查询已发布课程
     */
    @Query("SELECT c FROM Course c WHERE c.status = 'PUBLISHED' " +
           "AND (:categoryId IS NULL OR c.categoryId = :categoryId) " +
           "AND (:keyword IS NULL OR c.title LIKE %:keyword% OR c.description LIKE %:keyword%) " +
           "AND (:difficulty IS NULL OR c.difficulty = :difficulty) " +
           "AND (:minPrice IS NULL OR c.price >= :minPrice) " +
           "AND (:maxPrice IS NULL OR c.price <= :maxPrice) " +
           "ORDER BY " +
           "CASE WHEN :sortBy = 'price' THEN c.price END ASC, " +
           "CASE WHEN :sortBy = 'rating' THEN c.averageRating END DESC, " +
           "CASE WHEN :sortBy = 'enrollment' THEN c.enrollmentCount END DESC, " +
           "c.createdAt DESC")
    Page<Course> findCoursesWithFilters(@Param("categoryId") Long categoryId,
                                      @Param("keyword") String keyword,
                                      @Param("difficulty") Difficulty difficulty,
                                      @Param("minPrice") BigDecimal minPrice,
                                      @Param("maxPrice") BigDecimal maxPrice,
                                      @Param("sortBy") String sortBy,
                                      Pageable pageable);
    
    /**
     * 获取热门课程（按选课人数排序）
     */
    @Query("SELECT c FROM Course c WHERE c.status = 'PUBLISHED' ORDER BY c.enrollmentCount DESC, c.averageRating DESC")
    Page<Course> findPopularCourses(Pageable pageable);
    
    /**
     * 获取最新课程
     */
    @Query("SELECT c FROM Course c WHERE c.status = 'PUBLISHED' ORDER BY c.createdAt DESC")
    Page<Course> findLatestCourses(Pageable pageable);
    
    /**
     * 更新课程统计信息
     */
    @Modifying
    @Query("UPDATE Course c SET c.enrollmentCount = :enrollmentCount, " +
           "c.averageRating = :averageRating, c.reviewCount = :reviewCount WHERE c.id = :courseId")
    void updateCourseStats(@Param("courseId") Long courseId,
                          @Param("enrollmentCount") Integer enrollmentCount,
                          @Param("averageRating") BigDecimal averageRating,
                          @Param("reviewCount") Integer reviewCount);
    
    /**
     * 根据分类ID查询课程数量
     */
    long countByCategoryIdAndStatus(Long categoryId, CourseStatus status);
}

